
import java.io.IOException;
import java.util.Random;
import javax.microedition.lcdui.Graphics;
import javax.microedition.lcdui.Image;
import javax.microedition.lcdui.game.LayerManager;
import javax.microedition.lcdui.game.Sprite;

public class GameManager {

    private static Random Random;
    private static Sprite backgroundSprite;
    public static LayerManager DuckDie;
    public static LayerManager Waves;
    public static LayerManager Duck;
    public static LayerManager Background;
    private static Game MyGame;
    public static int Width;
    public static int Height;
    private static Image bg;
    private static int Timer;

    public static void StartWith(Game G) throws IOException {

        MyGame = G;
        Random = new Random(System.currentTimeMillis());
        DuckDie = new LayerManager();
        Duck = new LayerManager();
        Background = new LayerManager();
        Waves = new LayerManager();

        Width = MyGame.getWidth();
        Height = MyGame.getHeight() - 16;

        bg = Image.createImage("/resource/bg2.png");
        backgroundSprite = new Sprite(bg);
        Background.append(backgroundSprite);

        Timer = 1000;
    }

    public static int GetRandom(int min, int max) {
        return (Math.abs(Random.nextInt())) % (max - min) + min;
    }

    private static void DucksUpdate() {
        int Count = Duck.getSize() - 1;
        for (int i = Count; i >= 0; i--) {
            Duck Obj;
            try {
                Obj = ((Duck) Duck.getLayerAt(i));
                Obj.Move();
            } catch (Exception e) {
                continue;
            }
            for (int j = i - 1; j >= 0; j--) {
                Duck ObjC;
                try {
                    ObjC = ((Duck) Duck.getLayerAt(j));
                    if (Obj.collidesWith(ObjC, true)) {
                        Obj.Remove();
                        ObjC.Remove();
                        break;
                    }
                } catch (Exception e) {
                    continue;
                }
            }
        }
    }

    public static void WavesUpdate(int x, int y) {
        if (x == -1 || y == -1) {
            return;
        }
        int Count = Duck.getSize() - 1;
        for (int i = Count; i >= 0; i--) {
            Duck Obj;
            try {
                Obj = ((Duck) Duck.getLayerAt(i));
                Obj.Move();
            } catch (Exception e) {
                continue;
            }

            int x1 = Obj.getRefPixelX();
            int y1 = Obj.getRefPixelY();

            double d = (x - x1) * (x - x1) + (y - y1) * (y - y1);
            if (d < 400) {
                do {
                    Obj.SpeedX = -(Obj.SpeedX + GetRandom(0, 2));
                    Obj.SpeedY = -(Obj.SpeedY + GetRandom(0, 2));
                } while (Obj.SpeedX == 0 && Obj.SpeedY == 0);

                if (Obj.SpeedX < 0) {
                    Obj.isForward = false;
                    Obj.CurrentFrame = 5;
                } else {
                    Obj.isForward = true;
                     Obj.CurrentFrame = 1;
                }
            }
        }
    }

    private static void DrawBack(Graphics G) throws IOException {
        Timer--;
        G.setColor(255, 255, 255);
        G.fillRect(0, 0, Width, Height);
    }

    public static int soVit() {
        int Count = Duck.getSize();
        return Count;
    }

    public static void Update(Graphics G, int x, int y) throws IOException {
        DucksUpdate();
        WavesUpdate(x, y);
        DrawBack(G);

        Background.paint(G, 0, 0);
        G.setColor(255, 0, 0);
        G.drawString(String.valueOf(Timer * 2 / 100), 0, 0, 0);
        Duck.paint(G, 0, 0);
        DuckDie.paint(G, 0, 0);
        Waves.paint(G, 0, 0);
        if (Timer * 2 / 100 == 0) {
            G.setColor(255, 0, 0);
            G.drawString("YOU WIN!", 80, 100, 0);
            MyGame.isWinner = true;
        }

        if (MyGame.isGameover == true) {
            MyGame.Running = false;
            G.setColor(255, 0, 0);
            G.drawString("GAME OVER!", 80, 100, 0);
        }
        MyGame.flushGraphics();
    }

    public static void CreateDuck(int x, int y, int speed) throws IOException {
            Duck.append(new Duck(x, y, speed));
    }

    public static void CreateWaves(int x, int y) throws IOException {
        Waves.append(new Wave(x, y));
    }
}
